package com.manor.paradise.fragment.npcshop

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupAnimation
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.ClickUtil
import com.manor.common.utils.GsonUtil
import com.manor.common.utils.KLog
import com.manor.common.utils.ToastUtil
import com.manor.paradise.R
import com.manor.paradise.adapter.npcshop.NobleAdapter
import com.manor.paradise.bean.NobleBean
import com.manor.paradise.dialog.NoblePopup
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import kotlinx.android.synthetic.main.fragment_gifts.*
import java.math.BigDecimal

/**
 * 兑换贵族
 */
class ExchangeNobilityFragment(context: Context) : Fragment() {
    var mCurView: View? = null
    internal lateinit var mAdapter: NobleAdapter
    internal var pageNum = 1

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        mCurView = inflater.inflate(R.layout.fragment_gifts, container, false)
        return mCurView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        rcv_list.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageNum++
                setData()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageNum = 1
                setData()
            }
        })
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser) {
            pageNum = 1
            setData()
        }
    }

    private val itemChildClickListener = OnItemClickListener { adapter, view, position ->
        if (ClickUtil.canClick(500)) {


        if(null!=giftsBean){
            if(BigDecimal(giftsBean.level.toString()).compareTo(BigDecimal(mAdapter.getItem(position).level))==1){//当前等级大于购买等级
                ToastUtil.show("您的等级比购买等级高，不需要购买")

                return@OnItemClickListener
            }
            if(BigDecimal(giftsBean.level.toString()).compareTo(BigDecimal(mAdapter.getItem(position).level))==0){//当前等级等于购买等级

                    val ss = SpannableString("您已经是贵族，重复购买会续费当前等级")
                    val foregroundColorSpan = ForegroundColorSpan(Color.parseColor("#CE2F69"))
                    ss.setSpan(foregroundColorSpan, 12, 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                    XPopup.Builder(context)
                            .asConfirm(null, ss) {
                                val noblePopup = context?.let { NoblePopup(it, mAdapter.getItem(position)) }
                                XPopup.Builder(context)
                                        .hasShadowBg(true)
                                        .dismissOnTouchOutside(true)
                                        .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                                        .asCustom(noblePopup)
                                        .show()
                            }
                            .show()

                return@OnItemClickListener
            }
            if(BigDecimal(giftsBean.level.toString()).compareTo(BigDecimal(mAdapter.getItem(position).level))==-1){//当前等级小于购买等级
                if(giftsBean.level.toString().equals("0")){
                    val noblePopup = context?.let { NoblePopup(it,mAdapter.getItem(position)) }
                    XPopup.Builder(context)
                            .hasShadowBg(true)
                            .dismissOnTouchOutside(true)
                            .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                            .asCustom(noblePopup)
                            .show()
                }else {
                    val ss = SpannableString("您已经是贵族，重复购买将以高等级贵族有效期覆盖低等级贵族有效期。")
                    val foregroundColorSpan = ForegroundColorSpan(Color.parseColor("#CE2F69"))
                    ss.setSpan(foregroundColorSpan, 21, 23, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                    XPopup.Builder(context)
                            .asConfirm(null, ss) {
                                val noblePopup = context?.let { NoblePopup(it, mAdapter.getItem(position)) }
                                XPopup.Builder(context)
                                        .hasShadowBg(true)
                                        .dismissOnTouchOutside(true)
                                        .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                                        .asCustom(noblePopup)
                                        .show()
                            }
                            .show()
                }
                return@OnItemClickListener
            }

        }
        }

    }
    lateinit var giftsBean:NobleBean
    fun setData() {
        ElvesHttpUtil.getNoble(pageNum, object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }
                 giftsBean = GsonUtil.gsonToBean<NobleBean>(info, NobleBean::class.java)
                KLog.e("json", giftsBean);
                if (pageNum == 1) {
                    mAdapter = NobleAdapter(R.layout.item_npc_shop_nobility, giftsBean.nobleList)
                    mAdapter.setOnItemClickListener(itemChildClickListener)
                    rcv_list.setAdapter(mAdapter)
                    refreshLayout.finishRefresh()
                } else {
                    mAdapter.addData(giftsBean.nobleList)
                    refreshLayout.finishLoadMore()
                }

            }

            override fun onError() {
                super.onError()

            }
        })
    }   override fun onDestroy() {

        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_NPC_SHOP_LIST)
        super.onDestroy()
    }



}